var BMapLib = window.BMapLib = BMapLib || {};
(function() {
    BMapLib.COORD_TYPE_GPS = 0;
    BMapLib.COORD_TYPE_GOOGLE = 2;
    var c = 4;
    var a = 20;
    var b = BMapLib.MapWrapper = function(e, d) {
        this._map = e;
        this._coordType = d;
        this._hashObject = {};
        this._arrCache = []
    };
    b.prototype._uiqueNum = 0;
    b.prototype._canSendRequest = true;
    b.prototype.addOverlay = function(d) {
        if (!d || !(d instanceof BMap.Marker)) { return }
        var f = { overlay: d, uiqueNum: this._uiqueNum };
        this._arrCache.push(f);
        this._hashObject["guid" + this._uiqueNum] = d;
        this._uiqueNum++;
        var e = this;
        if (e._canSendRequest) {
            e._canSendRequest = false;
            window.setTimeout(function() {
                e._canSendRequest = true;
                e._delayRequest()
            }, 50)
        }
    };
    b.prototype._delayRequest = function() {
        var f = this._arrCache;
        var t = Math.ceil(f.length / a);
        for (var l = 0; l < t; l++) {
            var v = [],
                u = [],
                p = [];
            var m = l * a;
            var g = -1;
            if (f.length - (l + 1) * a > 0) { g = (l + 1) * a } else { g = f.length }
            for (var h = m; h < g; h++) {
                var k = f[h];
                var s = k.overlay.getPosition().lng;
                var r = k.overlay.getPosition().lat;
                v.push(s);
                u.push(r);
                p.push("guid" + k.uiqueNum)
            }
            var q = v.join(",");
            var o = u.join(",");
            var e = { x: q, y: o, from: this._coordType, to: c, mode: 1 };
            var d = { guids: p };
            var n = this;
            b._SearchRequestMgr.request(function(j, i) { n._requestCbk(j, i) }, e, d)
        }
        this._arrCache.length = 0
    };
    b.prototype._requestCbk = function(m, f) {
        if (m && m instanceof Array) {
            var j = f.guids;
            for (var g = 0; g < m.length; g++) {
                var l = m[g];
                if (l && l.error == 0) {
                    var d = l.x,
                        n = l.y;
                    var e = new BMap.Point(d, n);
                    var k = j[g];
                    if (this._hashObject[k]) {
                        var h = this._hashObject[k];
                        h.setPosition(e);
                        this._map.addOverlay(h);
                        delete this._hashObject[k]
                    }
                }
            }
        }
    };
    b._jsonToQuery = function(d, f) {
        var e = [];
        f = f || function(h) { return h };
        for (var g in d) { e.push(g + "=" + f(d[g])) }
        return e.join("&")
    };
    b._SearchRequestMgr = {
        COORD_CONVERT_URL: "http://api.map.baidu.com/ag/coord/convert",
        request: function(e, k, d, j) {
            var f = (Math.random() * 100000).toFixed(0);
            b._cbkMount["_cbk" + f] = function(l) {
                d = d || {};
                e && e(l, d);
                delete b._cbkMount["_cbk" + f]
            };
            j = j || "";
            var h = b._jsonToQuery(k, encodeURIComponent);
            var i = this,
                g = i.COORD_CONVERT_URL + j + "?" + h + "&ie=utf-8&oue=1&res=api&callback=BMapLib.MapWrapper._cbkMount._cbk" + f;
            i.createScript(g)
        },
        createScript: function(e) {
            var d = document.createElement("script");
            d.src = e;
            d.setAttribute("type", "text/javascript");
            d.setAttribute("charset", "utf-8");
            if (d.addEventListener) {
                d.addEventListener("load", function(g) {
                    var f = g.target;
                    f.parentNode.removeChild(f)
                }, false)
            } else { if (d.attachEvent) { d.attachEvent("onreadystatechange", function(g) { var f = window.event.srcElement; if (f && (f.readyState == "loaded" || f.readyState == "complete")) { f.parentNode.removeChild(f) } }) } }
            setTimeout(function() {
                document.getElementsByTagName("head")[0].appendChild(d);
                d = null
            }, 1)
        }
    };
    b._cbkMount = {};
    window._BMapLibStatImg = "http://api.map.baidu.com/images/";
    window._addStat = function(j, h) {
        if (!j) { return }
        h = h || {};
        var g = "";
        for (var e in h) { g = g + "&" + e + "=" + encodeURIComponent(h[e]) }
        var k = function(i) {
            if (!i) { return }
            _addStat._sending = true;
            setTimeout(function() { _addStat._img.src = window._BMapLibStatImg + "blank.gif?" + i.src }, 50)
        };
        var d = function() { var i = _addStat._reqQueue.shift(); if (i) { k(i) } };
        var f = (Math.random() * 100000000).toFixed(0);
        if (_addStat._sending) { _addStat._reqQueue.push({ src: "t=" + f + "&code=" + j + g }) } else { k({ src: "t=" + f + "&code=" + j + g }) }
        if (!_addStat._binded) {
            _addStat._img.onload = function() {
                _addStat._sending = false;
                d()
            };
            _addStat._img.onerror = function() {
                _addStat._sending = false;
                d()
            };
            _addStat._binded = true
        }
    };
    window._addStat._reqQueue = [];
    window._addStat._img = new Image();
    window._addStat(5015)
})();